package cn.vc.content.service.video;

import cn.vc.content.entity.video.VideoReview;
import cn.vc.content.mapper.video.VideoReviewMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class VideoReviewService {
    @Resource
    private VideoReviewMapper videoReviewMapper;


    /**
     * 评论
     * @param videoReview
     * @return
     */
    public boolean review(VideoReview videoReview) {
        return videoReviewMapper.insert(videoReview)==1;
    }


    /**
     * 查询评论回复及回复。。。
     * @param videoReviewList
     * @return
     */
    public List<VideoReview> selectRecover(List<VideoReview> videoReviewList) {
        for (VideoReview videoReview : videoReviewList) {
            // 查询评论回复
            LambdaQueryWrapper<VideoReview> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(VideoReview::getReviewId, videoReview.getId());
            List<VideoReview> videoReviewList1 = videoReviewMapper.selectList(lqw1);
            // 如果评论的回复为空，则无需查询该回复的回复
            if (videoReviewList1!=null) {
                selectRecover(videoReviewList1);
                // 反转list
                Collections.reverse(videoReviewList1);
                videoReview.setVideoRecoverList(videoReviewList1);
            }
        }
        return videoReviewList;
    }
    /**
     * 查询视频评论
     */
    public List<VideoReview> selectReview(Long videoId) {
        // 查询视频回复
        LambdaQueryWrapper<VideoReview> lqw = new LambdaQueryWrapper<>();
        lqw.eq(VideoReview::getVideoId, videoId);
        List<VideoReview> videoReviewList = videoReviewMapper.selectList(lqw);
        // 反转list
        Collections.reverse(videoReviewList);

        videoReviewList = selectRecover(videoReviewList);

        return videoReviewList;
    }
}
